package io.github.lee.jel.ui.list

import android.graphics.Color
import android.util.TypedValue
import androidx.core.os.bundleOf
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.fondesa.recyclerviewdivider.dividerBuilder
import dagger.hilt.android.AndroidEntryPoint
import io.github.lee.core.ui.BaseRefreshFragment
import io.github.lee.core.util.getArgumentParcelable
import io.github.lee.core.util.getColor
import io.github.lee.core.util.isScreenLand
import io.github.lee.jel.R
import io.github.lee.jel.ui.detail.ItemDetailActivity
import io.github.lee.repository.jel.domain.Item

@AndroidEntryPoint
class ItemListFragment : BaseRefreshFragment<ItemListVM>() {

    companion object {
        private const val KEY_PARENT_ITEM = "ITEM"
        fun newInstance(parent: Item): ItemListFragment {
            val bundle = bundleOf(
                KEY_PARENT_ITEM to parent
            )
            val f = ItemListFragment()
            f.arguments = bundle
            return f
        }
    }

    override fun onCreateLayoutManager(): RecyclerView.LayoutManager {
        val spanCount = if (isScreenLand(mContext)) {
            7
        } else {
            3
        }

        return GridLayoutManager(mContext, spanCount)
            .apply {
                spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
                    override fun getSpanSize(position: Int): Int =

                        if (position == ((mAdapterHelper?.adapter?.adapters?.size
                                ?: 0) + (vm?.adapter?.items?.size ?: 0) - 1) - 1
                        ) {
                            spanCount
                        } else {
                            1
                        }
                }
            }

    }

    //===Desc:=============================================================
    override fun onCreateViewModel(): Lazy<ItemListVM> =
        viewModels()


    //===Desc:=============================================================

    override fun onSetViewListener() {
        super.onSetViewListener()
        vm?.adapter?.setOnItemClickListener { _, _, position ->
            val item = vm?.getItem(position)
            if (null == item) {
                toast(R.string.error_application_error)
                return@setOnItemClickListener
            }
            if (item.isFolder == true) {
                ItemListActivity.start(mContext, item)
            } else {
                ItemDetailActivity.start(mContext, item)
            }
        }
    }

    override fun onSetViewData() {
        super.onSetViewData()
        val parent = getArgumentParcelable(KEY_PARENT_ITEM, Item::class.java)
        if (null == parent) {
            toast(getString(R.string.error_param_missing))
            return
        }
        vm?.parentItem = parent
        addItemDecoration(
            mContext.dividerBuilder()
                .color(Color.TRANSPARENT)
                .size(8, TypedValue.COMPLEX_UNIT_DIP)
                .showSideDividers()
                .build()
        )
        recyclerView?.setBackgroundColor(getColor(R.color.main_page_background))
    }
}